/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PWS;

/**
 *
 * @author LBAOMELM
 */
public class Caesar {

    final String ALFABET = "abcdefghijklmnopqrstuvwxyz";
    String ENCRYPT = null;
    int KEY = -1;

    public Caesar(String e) {
        this.ENCRYPT = e;
    }

    public Caesar(String e, int k) {
        this.ENCRYPT = e;
        this.KEY = k;
    }

    public void setKey(int k) {
        this.KEY = k;
    }

    public void setEncrypt(String s) {
        this.ENCRYPT = s;
    }

    public String encrypt() {
        String res = "";
        String s = "";
        if (ENCRYPT == null || ENCRYPT.length() == 0) {
            return null;
        }

        for (int i = 0; i < ENCRYPT.length(); i++) {
            s += ENCRYPT.charAt(i);
            if (!Character.toString(ENCRYPT.charAt(i)).equals(" ")) {
                res += ALFABET.charAt(Methods.mod(
                        (Methods.charToNum(ENCRYPT.charAt(i)) + KEY), 26));
            } else {
                res += " ";
            }
            //System.out.println(s + " | " + res);
        }
        return res;
    }

    public String decrypt() {
        String res = "";
        if (ENCRYPT == null || ENCRYPT.length() == 0) {
            return null;
        }

        for (int i = 0; i < ENCRYPT.length(); i++) {
            if (!Character.toString(ENCRYPT.charAt(i)).equals(" ")) {
                res += ALFABET.charAt(Methods.mod(
                        (Methods.charToNum(ENCRYPT.charAt(i)) - KEY), 26));
            } else {
                res += " ";
            }
        }
        return res;
    }
}
